<template>
  <div class="list" ref="wrapper">
    <div>
      <div class="area">
        <div class="title border-topbottom">您的位置</div>
        <div class="list-button">
          <div class="button-wrapper">
            <div class="button">{{ this.city }}</div>
          </div>
        </div>
      </div>
      <div class="area">
        <div class="title border-topbottom">热门城市</div>
        <div class="list-button">
          <div class="button-wrapper" v-for="item in hot" :key="item.id">
            <div class="button" @click="handlechangecity(item.name)">
              {{ item.name }}
            </div>
          </div>
        </div>
      </div>

      <div class="area" :ref="key" v-for="(item, key) in cities" :key="key">
        <div class="title border-topbottom">{{ key }}</div>
        <ul class="list-item">
          <li
            class="item border-bottom"
            v-for="innerItem of item"
            :key="innerItem.id"
            @click="handlechangecity(innerItem.name)"
          >
            {{ innerItem.name }}
          </li>
        </ul>
      </div>
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import Bscroll from 'better-scroll'
export default {
  name: 'CityList',
  props: {
    hot: Array,
    cities: Object
  },
  computed: {
    ...mapState({
      city: (state) => state.city.city
    })
  },

  data() {
    return {
      letter: ''
    }
  },

  mounted() {
    this.scroll = new Bscroll(this.$refs.wrapper)
    this.$bus.$on('letterChange', (broindex) => {
      this.letter = broindex
    })
  },
  watch: {
    letter() {
      if (this.letter) {
        const element = this.$refs[this.letter][0]
        this.scroll.scrollToElement(element)
      }
    }
  },

  methods: {
    handlechangecity(bro) {
      alert(bro)
      this.$store.dispatch('changeCity', bro)
      this.$router.push('/')
    }
  }
}
</script>

<style lang="less" scoped>
.list {
  overflow: hidden;
  position: absolute;
  top: 2rem;
  left: 0;
  bottom: 0;
  right: 0;

  .title {
    background: #eee;
    width: 100%;
    height: 0.5rem;
    line-height: 0.5rem;
    color: #666;
    font-size: 0.26rem;
    padding-left: 0.2rem;
  }
  .border-topbottom {
    &:before {
      border-color: #ccc;
    }
    &:after {
      border-color: #ccc;
    }
  }
  .border-bottom {
    &:before {
      border-color: #ccc;
    }
  }

  .list-button {
    background: #fff;
    padding: 0.1rem;
    display: flex;
    flex-wrap: wrap;

    .button-wrapper {
      float: left;
      width: 33.33%;
      padding: 0.1rem 0.2rem;
      box-sizing: border-box;
      .button {
        padding: 0.2rem 0;
        text-align: center;
        border: 0.02rem solid #00bcd4;
        border-radius: 0.06rem;

        color: #00bcd4;
      }
    }
  }

  .item {
    height: 0.86rem;
    line-height: 0.86rem;
    padding-left: 0.2rem;
  }
}
</style>
